package com.itmuch.cloud.controller;

import com.google.common.collect.Maps;
import com.itmuch.cloud.dto.UserDTO;
import com.itmuch.cloud.pojo.User;
import com.itmuch.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
public class MovieController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }

    @GetMapping("/users/{id}/orders/{orderId}")
    public String getOrder(@PathVariable("id") Long userId, @PathVariable("orderId") Long orderId) {
        return userService.getOrder(userId, orderId);
    }

    @GetMapping("/search1")
    String search1(@RequestParam("name") String name, @RequestParam("age") Integer age, @RequestParam("size") Integer size) {
        return userService.search1(name, age, size);
    }

    @GetMapping("/search2")
    String search2(@RequestParam("name") String name, @RequestParam("age") Integer age, @RequestParam("size") Integer size) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("name", name);
        params.put("age", age);
        params.put("size", size);
        return userService.search2(params);
    }

    @PostMapping("/users")
    String createUser(@RequestBody UserDTO user) {
        return userService.createUser(user);
    }

    /**
     * 单文件接收
     *
     * @param file
     * @param description
     * @return
     */
    @PostMapping("/upload")
    public String handleUpload(@RequestPart("file") MultipartFile file, @RequestPart("description") String description) {
        return userService.uploadFile(file, description);
    }
}
